package JFrame;

import Class.User;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

// 登陆界面
public class  LoginJFrame extends JFrame implements ActionListener {
    JButton btnLogin = new JButton("登录");
    JButton btnRegister = new JButton("注册");
    JLabel labelUserName = new JLabel("用户名称");
    JTextField tfdUserName = new JTextField(12);
    JLabel labelPassword = new JLabel("用户密码");
    JPasswordField tfdPassword = new JPasswordField(12);
    public static User user;
    public static void initUser(String s1, String s2, String s3, String s4, String s5,
                                String s6, double d1, double d2) {
        user = new User(s1, s2, s3, s4, s5, s6, d1, d2);
    }
    public static User getUser() {
        return user;
    }
    // 登录界面
    public LoginJFrame(){
        this.setName("村镇银行业务储蓄系统");
        this.setTitle("登录界面");
        this.setSize(300, 200);
        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(3);
        this.setLayout(new FlowLayout());

        JPanel p1 = new JPanel();
        p1.add(labelUserName);
        p1.add(tfdUserName);

        JPanel p2 = new JPanel();
        tfdPassword.setEchoChar('*');
        p2.add(labelPassword);
        p2.add(tfdPassword);

        JPanel p3 = new JPanel();
        p3.add(btnLogin);
        p3.add(btnRegister);

        this.add(p1);
        this.add(p2);
        this.add(p3);
        this.setVisible(true);

        btnLogin.addActionListener(this);
        btnRegister.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e){
        // 登录
        if(e.getSource() == btnLogin){
            try {
                // 去匹配账号和密码
                matchUser();
            }catch (Exception exception){
                System.out.println("登录时出现异常");
            }
        }
        // 注册
        else if(e.getSource() == btnRegister){
            // 进入注册页面
            RegisterJFrame registerJFrame = new RegisterJFrame();
        }
    }

    public void matchUser() throws Exception{
        String accountText = tfdUserName.getText();
        String passText = new String(tfdPassword.getPassword());

        Statement statement = null;
        ResultSet result = null;
        Connection connection = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String ur1 = "jdbc:mysql://localhost:3306/bank"; // bank为数据库名
            String username = "root";               //数据库用户名
            String mypassword = "20222746";          //数据库密码
            connection = DriverManager.getConnection(ur1, username, mypassword);
            statement = connection.createStatement();
            String sql = "select * from user";
            result = statement.executeQuery(sql);

            while (result.next()) {
                String hasUserName = result.getNString("username");
                String hasPassword = result.getNString("password");
                String hasTelephone = result.getNString("telephone");
                String hasID = result.getNString("ID");
                String hasName = result.getNString("name");
                String hasCardNumber = result.getNString("cardNumber");
                double hasDemandMoney = result.getDouble("demandMoney");
                double hasTimeMoney = result.getDouble("timeMoney");
                // 匹配上账号和密码
                if (hasUserName.equals(accountText) && hasPassword.equals(passText)) {
                    // 进入用户界面
                    initUser(hasUserName, hasPassword, hasTelephone, hasID, hasName,
                            hasCardNumber, hasDemandMoney, hasTimeMoney);
                    UserJFrame userJFrame = new UserJFrame();
                    break;
                }
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }finally {
            if(result!=null){
                try{
                    result.close();
                }catch (SQLException e){
                    e.printStackTrace();
                }
                statement = null;
            }
            if(connection != null){
                try{
                    connection.close();
                }catch (SQLException e){
                    e.printStackTrace();
                }
                connection = null;
            }
        }
    }
}
